﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using ProgNetComponents.Collections;
using System.Drawing.Design;
using ProgNetComponentsDx.Options.Design;
using System.Drawing;

namespace ProgNetComponentsDx.Options
{
    [TypeConverter(typeof(OptionGroupTypeConverter))]
    public class OptionGroup : ProgNetComponents.Interfaces.INamedGroup
    {
        private string _Name;
        private OptionGroup _Parent;
        private OptionGroupCollection _Groups;
        private OptionCollection _Items = new OptionCollection();

        public OptionGroup()
            : this("", null, null)
        {

        }

        public OptionGroup(string name, OptionGroup[] childGroups , Option[] options )
        {
            _Groups = new OptionGroupCollection(this);
            if (childGroups != null)
            {
                foreach (OptionGroup o in childGroups)
                {
                    o._Parent = this;
                    _Groups.Add(o);
                }
            }
            if (options != null)
            {
                _Items.AddRange(options);
            }
            Name = name;
        }
        
        public OptionGroup(string name, OptionGroup[] childGroups)
            : this(name, childGroups, null)
        {

        }
        public OptionGroup(string name)
            : this(name, null, null)
        {

        }
        public OptionGroup(string name, Option[] options)
            : this(name, null, options)
        {

        }

        public string Name
        {
            get { return _Name; }
            set { _Name = ProgNetComponents.Tools.ConstHelper.GetFriendlyName(value); }
        }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public OptionGroupCollection ChildGroups
        {
            get { return _Groups; }
        }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public OptionCollection Options
        {
            get { return _Items; }
        }

        public override string ToString()
        {
            string str = Name;
            return str;
        }

        #region INamedGroup Members

        System.Collections.IList ProgNetComponents.Interfaces.INamedGroup.Groups
        {
            get { return _Groups; }
        }

        System.Collections.IList ProgNetComponents.Interfaces.INamedGroup.Items
        {
            get { return _Items; }
        }
        
        bool ProgNetComponents.Interfaces.INamedGroup.HasAnyItems()
        {
            if (Options.Count > 0)
                return true;
            foreach (ProgNetComponents.Interfaces.INamedGroup g in ChildGroups)
                if (g.HasAnyItems())
                    return true;
            return false;
        }
        #endregion

    }

    [Editor(typeof(Design.OptionGroupCollectionEditor), typeof(UITypeEditor))]
    public class OptionGroupCollection : BaseNamedCollection<OptionGroup>
    {
        private OptionGroup _Parent;
        public OptionGroup Parent
        {
            get { return _Parent; }
        }
        public OptionGroupCollection(OptionGroup parent = null)
        {
            _Parent = parent;
        }
        protected override void InsertItem(int index, OptionGroup item)
        {
            int count = this.Count;
            base.InsertItem(index, item);
        }
    }
}
